package com.bjzsly.star3.xzb.plugin;


import android.Manifest;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.bjzsly.star3.shareclick.utils.WeChatUtils;
import com.bjzsly.star3.xzb.BuildConfig;
import com.bjzsly.star3.xzb.auth.TxShareUtils;
import com.bjzsly.star3.xzb.utils.DeviceUtil;
import com.bjzsly.star3.xzb.utils.PermissionUtil;
import com.bjzsly.star3.xzb.utils.ToastUtils;
import com.sc.sdk.Utils;

import org.json.JSONArray;

import java.util.HashMap;
import java.util.UUID;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
 * 5+ SDK 扩展插件示例
 * 5+ 扩扎插件在使用时需要以下两个地方进行配置
 * 		1  WebApp的mainfest.json文件的permissions节点下添加JS标识
 * 		2  assets/data/properties.xml文件添加JS标识和原生类的对应关系
 * 本插件对应的JS文件在 assets/apps/H5Plugin/js/test.js
 * 本插件对应的使用的HTML assest/apps/H5plugin/index.html
 *
 * 更详细说明请参考文档http://ask.dcloud.net.cn/article/66
 * **/
public class PGPlugiUtil extends StandardFeature {

//    public static IWebview mIWebView;
//    public static String backPressCallBack;

    public void onStart(Context pContext, Bundle pSavedInstanceState, String[] pRuntimeArgs) {
        /**
         * 如果需要在应用启动时进行初始化，可以继承这个方法，并在properties.xml文件的service节点添加扩展插件的注册即可触发onStart方法
         * */
    }

    //    // 需要手动调用
//    public void init(IWebview webView, JSONArray array) {
//        mIWebView = webView;
//        backPressCallBack = array.optString(0);
//    }

    public void shareQQ(IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        //,String title, String summary,  String imgUrl,String targetUrl

        if (TxShareUtils.getInstance().isQQInstall(pWebview.getActivity())){
            String CallBackID = array.optString(0);
            try{
                String title = array.optString(1);
                String summary = array.optString(2);
                String imgUrl = array.optString(3);
                String targetUrl = array.optString(4);
                TxShareUtils.getInstance().shareQQ(pWebview.getActivity(),title,summary,imgUrl,targetUrl);
            }catch (Exception e){
            }
        }else {
            ToastUtils.showToast("请先安装QQ");
        }
    }

    public void shareWX(final IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        //,String title, String summary,  String imgUrl,String targetUrl
        if (TxShareUtils.getInstance().isWxInstall()) {
            final String CallBackID = array.optString(0);
            final String title = array.optString(1);
            final String summary = array.optString(2);
            final String imgUrl = array.optString(3);
            final String targetUrl = array.optString(4);
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        WeChatUtils.configType(pWebview.getActivity(), WeChatUtils.TYPE_SHARE_WX_FRIEDN);
                        TxShareUtils.getInstance().shareFriHtml(pWebview.getActivity(), title, summary, imgUrl, targetUrl);
                    } catch (Exception e) {
                        Log.e("Xiang", e.getMessage());
                    }
                }
            }.start();

        }else {
            ToastUtils.showToast("请先安装微信");
        }
    }


    public  void getMsg(IWebview pWebview,JSONArray array){
        String CallBackID = array.optString(0);
        UUID uuid;
        String androidId = getAndroidId(pWebview.getActivity());
        if (androidId != null && !"9774d56d682e549c".equals(androidId)) {
            uuid = UUID.nameUUIDFromBytes(androidId.getBytes());
        } else {
            final String deviceId = getDeviceId(pWebview.getActivity());
            uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes()) : UUID.randomUUID();
        }
        Log.d("Xiang",uuid.toString());
        String  yqm = "0";
        String  flag = "1";
        try {
            String pkgUuid = Utils.readPkgUuid(pWebview.getActivity());
            HashMap<String, String> map = Utils.parserJson(pkgUuid);
            if (map!=null && map.size()>0){
                yqm = map.get("invit_code");
                flag = map.get("flag");
            }
        }catch (Exception e){
            Log.e("Xiang",e.getMessage());
        }
        Log.e("Xiang",flag);
        Log.e("Xiang",yqm);
       String cid =uuid.toString() ;
       String uid =uuid.toString() ;
        JSONArray newArray = new JSONArray();
        newArray.put(cid);
        newArray.put(uid);
        newArray.put(yqm);
        newArray.put(flag);
        // 调用方法将原生代码的执行结果返回给js层并触发相应的JS层回调函数
        JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);
    }

    public void shareCircle(final IWebview pWebview, JSONArray array) {  //不带参数 无法成功回调
        if (TxShareUtils.getInstance().isWxInstall()) {
        final String CallBackID = array.optString(0);
        final String title = array.optString(1);
        final String summary = array.optString(2);
        final String imgUrl = array.optString(3);
        final String targetUrl = array.optString(4);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    WeChatUtils.configType(pWebview.getActivity(),WeChatUtils.TYPE_SHARE_WX_CIRCLE);
                    TxShareUtils.getInstance().shareCirHtml(pWebview.getActivity(),title,summary,imgUrl,targetUrl);
                }catch (Exception e){
                    Log.e("Xiang",e.getMessage());
                }
            }
        }.start();
    }else {
            ToastUtils.showToast("请先安装微信");
        }
    }


    public  void randomDeviceUUID(IWebview pWebview, JSONArray array) {
         String CallBackID = array.optString(0);
        UUID uuid;
        final String androidId = getAndroidId(pWebview.getActivity());
        if (androidId != null && !"9774d56d682e549c".equals(androidId)) {
            uuid = UUID.nameUUIDFromBytes(androidId.getBytes());
        } else {
            final String deviceId = getDeviceId(pWebview.getActivity());
            uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes()) : UUID.randomUUID();
        }
        Log.d("Xiang",uuid.toString());
        JSUtil.execCallback(pWebview, CallBackID, uuid.toString(), JSUtil.OK, false);
    }


    public static String getAndroidId(Context context) {
        try {
            return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } catch (Exception e) {
            Log.e("Xiang",e.getMessage());
        }
        return null;
    }

    public static String getDeviceId(Context context) {
        if (PermissionUtil.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                return checkDeviceId(tm.getDeviceId());
            } catch (Exception e) {
               Log.e("Xiang",e.getMessage());
            }
        }
        return null;
    }

    private static String checkDeviceId(String deviceId) {
        if (!TextUtils.isEmpty(deviceId))
            return deviceId;
        try {
            return "35" +
                    Build.BOARD.length() % 10 +
                    Build.BRAND.length() % 10 +
                    Build.CPU_ABI.length() % 10 +
                    Build.DEVICE.length() % 10 +
                    Build.DISPLAY.length() % 10 +
                    Build.HOST.length() % 10 +
                    Build.ID.length() % 10 +
                    Build.MANUFACTURER.length() % 10 +
                    Build.MODEL.length() % 10 +
                    Build.PRODUCT.length() % 10 +
                    Build.TAGS.length() % 10 +
                    Build.TYPE.length() % 10 +
                    Build.USER.length() % 10;
        } catch (Exception e) {
           Log.e("Xiang",e.getMessage());
        }
        return null;
    }
    public String umeng_channel = "UMENG_CHANNEL";
    public  void getChannel(IWebview pWebview, JSONArray array) {
        String CallBackID = array.optString(0);
        try {
//            String channel = array.optString(1);
            String appChannel = DeviceUtil.getAppChannel(pWebview.getActivity(), umeng_channel);
            JSUtil.execCallback(pWebview, CallBackID,appChannel , JSUtil.OK, false);
        }catch (Exception e){
            Log.e("Xiang",e.getMessage());
            JSUtil.execCallback(pWebview, CallBackID,"" , JSUtil.ERROR, false);
        }
    }

    public  void getYQM(IWebview pWebview, JSONArray array) {
        String CallBackID = array.optString(0);
        try {
            JSUtil.execCallback(pWebview, CallBackID, BuildConfig.yqm, JSUtil.OK, false);
        }catch (Exception e){
            Log.e("Xiang",e.getMessage());
            JSUtil.execCallback(pWebview, CallBackID,"" , JSUtil.ERROR, false);
        }
    }
    public  void getFlag(IWebview pWebview, JSONArray array) {
        String CallBackID = array.optString(0);
        try {
            JSUtil.execCallback(pWebview, CallBackID, BuildConfig.flag, JSUtil.OK, false);
        }catch (Exception e){
            Log.e("Xiang",e.getMessage());
            JSUtil.execCallback(pWebview, CallBackID,"" , JSUtil.ERROR, false);
        }
    }

    public  void setWxType(IWebview pWebview, JSONArray array) {
            WeChatUtils.configType(pWebview.getActivity(),WeChatUtils.TYPE_WX_AUTH_LOGIN);
    }
}

